home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / buffer / bufferglm.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.3 KB  |  393 lines

  1. /*
  2.  * Copyright (C) 1991, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  */
  5. /*
  6.  * bufferglm:  mixed model demo (Motif-based) shows how to switch back and
  7.  *             forth between single and double -buffer mode.
  8.  *
  9.  *       left mouse  :  switch to single buffer
  10.  *       middle mouse:  switch to double buffer
  11.  *       right mouse :  animate the green sphere in the current buffer mode
  12.  *
  13.  *       the program handles redraw (move, resize) and expose (pop, expose,
  14.  *       de-iconify) events.  ESC key exits the program.
  15.  *
  16.  *                                   Yusuf Attarwala  -- September, 1991
  17.  */
  18. #include <X11/Intrinsic.h>
  19. #include <Xm/Xm.h>
  20. #include <Xm/Frame.h>
  21. #include <X11/Xirisw/GlxMDraw.h>
  22.  
  23. #include <gl.h>
  24. #include <gl/sphere.h>
  25.  
  26. /* function declarations */
  27.  
  28. void initialize();
  29. void exitAC();
  30. void installColormap();
  31. void createToplevel();
  32. void createSingleBuffer();
  33. void createDoubleBuffer();
  34. void toSingleBuffer();
  35. void toDoubleBuffer();
  36. void bindLight();
  37. void setMatrix();
  38. void loop();
  39. void drawScene();
  40. void exposeCB();
  41. void resizeCB();
  42. void initCB();
  43. void inputCB();
  44.  
  45.  
  46. static float material1[] = {
  47.     DIFFUSE,  0.6,0.2,0.2,
  48.     SPECULAR, 1.0,1.0,1.0,
  49.     SHININESS, 120.0,
  50.     LMNULL
  51. };
  52. static float material2[] = {
  53.     DIFFUSE,  0.2,0.8,0.2,
  54.     SPECULAR, 1.0,1.0,1.0,
  55.     SHININESS, 120.0,
  56.     LMNULL
  57. };
  58. static float infinite[] = { 
  59.     AMBIENT,      0.3,0.3,0.3,
  60.     LOCALVIEWER,  0.0,
  61.     LMNULL 
  62. };
  63.  
  64. static float white_light[] = {
  65.     AMBIENT,  0.2, 0.2, 0.2,
  66.     LCOLOR,   0.9, 0.9, 0.9,
  67.     POSITION, 10.0,50.0,50.0,0.0,
  68.     LMNULL 
  69. };
  70.  
  71.  
  72. static char defaultTranslations[] =
  73.    "<KeyUp>osfCancel:  quit()        ";  
  74.  
  75. static XtActionsRec actionsTable[] = {
  76.     {"quit",exitAC},
  77. };
  78.  
  79. Display      *display;
  80. XtAppContext appContext;
  81. XtTranslations transTable;
  82. long          doubleBuffer;
  83. Angle         ax,ay,az;
  84. static Matrix iden = {1.0,0.,0.,0.,0.,1.0,0.,0.,0.,0.,1.0,0.,0.,0.,0.,1.0};
  85.  
  86. Widget       toplevel,       /* toplevel shell */
  87.          frameSB,        /* frame to hold single buffer gl widget */
  88.          frameDB,        /* frame to hold double buffer gl widget */
  89.          glwSB,
  90.          glwDB,
  91.          glw;            /* current widget */
  92.  
  93. Arg args[20];
  94. int acnt;
  95.  
  96. main(argc,argv)
  97. int argc;
  98. char **argv;
  99. {
  100.     XtToolkitInitialize();
  101.     appContext = XtCreateApplicationContext();
  102.     display    = XtOpenDisplay(appContext, NULL, "Buffer","buffer",NULL,0,
  103.                 &argc,argv);
  104.     if (!display) {
  105.     printf("%s : Unable to open display\n",argv[0]);
  106.     exit(0);
  107.     }
  108.  
  109.     XtAppAddActions(appContext, actionsTable, XtNumber(actionsTable));
  110.     transTable = XtParseTranslationTable(defaultTranslations);
  111.  
  112.     createToplevel();
  113.     XtAppMainLoop(appContext);
  114. }
  115.  
  116. void 
  117. installColormap()
  118. {
  119.     Window windows[3];
  120.  
  121.     windows[0] = XtWindow(toplevel);
  122.     windows[1] = XtWindow(glwDB);
  123.     windows[2] = XtWindow(glwSB);
  124.     XSetWMColormapWindows(display, XtWindow(toplevel), windows, 3);
  125.  
  126. }
  127.  
  128. void
  129. createToplevel()
  130. {
  131.     acnt = 0;
  132.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  133.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  134.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  135.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  136.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  137.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  138.     toplevel  = XtAppCreateShell("toplevel","buffer",
  139.                                   applicationShellWidgetClass,
  140.                                   display,args,acnt);
  141.  
  142.     createDoubleBuffer();
  143.     XtRealizeWidget(toplevel);
  144.     initialize();
  145.     createSingleBuffer();
  146.     installColormap();
  147.  
  148.     toSingleBuffer();
  149.     installColormap();
  150. }
  151.  
  152. void
  153. createSingleBuffer()
  154. {
  155.     static GLXconfig glxConfigS [] = {
  156.         { GLX_NORMAL, GLX_DOUBLE, FALSE },
  157.         { GLX_NORMAL, GLX_RGB, TRUE },
  158.         { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  159.         { 0, 0, 0 }
  160.     };
  161.  
  162.     acnt = 0;
  163.     frameSB = XtCreateWidget("frameSB", xmFrameWidgetClass,
  164.                               toplevel, args, acnt);
  165.  
  166.     /* NOTE : do not manage child here */
  167.  
  168.     acnt = 0;
  169.     XtSetArg(args[acnt], GlxNglxConfig, glxConfigS); acnt++;
  170.     glwSB = XtCreateWidget("glwSB", glxMDrawWidgetClass,
  171.                             frameSB, args, acnt);
  172.  
  173.     XtManageChild(glwSB);
  174.  
  175.     XtAddCallback(glwSB, GlxNexposeCallback, exposeCB, NULL);
  176.     XtAddCallback(glwSB, GlxNresizeCallback, resizeCB, NULL);
  177.     XtAddCallback(glwSB, GlxNginitCallback,  initCB,   NULL);
  178.     XtAddCallback(glwSB, GlxNinputCallback,  inputCB,  NULL);
  179.     XtOverrideTranslations(glwSB, transTable);
  180.  
  181. }
  182.  
  183. void
  184. createDoubleBuffer()
  185. {
  186.     static GLXconfig glxConfigD [] = {
  187.         { GLX_NORMAL, GLX_DOUBLE, TRUE },
  188.         { GLX_NORMAL, GLX_RGB, TRUE },
  189.         { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  190.         { 0, 0, 0 }
  191.     };
  192.  
  193.     acnt = 0;
  194.     frameDB = XtCreateWidget("frameDB", xmFrameWidgetClass,
  195.                               toplevel, args, acnt);
  196.  
  197.     XtManageChild(frameDB);
  198.  
  199.     acnt = 0;
  200.     XtSetArg(args[acnt], GlxNglxConfig, glxConfigD); acnt++;
  201.     glwDB = XtCreateWidget("glwDB", glxMDrawWidgetClass,
  202.                             frameDB, args, acnt);
  203.     XtManageChild(glwDB);
  204.  
  205.     XtAddCallback(glwDB, GlxNexposeCallback, exposeCB, NULL);
  206.     XtAddCallback(glwDB, GlxNresizeCallback, resizeCB, NULL);
  207.     XtAddCallback(glwDB, GlxNginitCallback,  initCB,   NULL);
  208.     XtAddCallback(glwDB, GlxNinputCallback,  inputCB,  NULL);
  209.     XtOverrideTranslations(glwDB, transTable);
  210.  
  211. }
  212.  
  213. void
  214. resizeCB(w,client_data,glx_struct)
  215. Widget w;
  216. caddr_t client_data;
  217. GlxDrawCallbackStruct *glx_struct;
  218. {
  219.     GLXwinset(XtDisplay(w), XtWindow(w));
  220.     viewport(0, (Screencoord) glx_struct->width-1,
  221.              0, (Screencoord) glx_struct->height-1);
  222.  
  223.     drawScene();
  224.  
  225. }
  226.  
  227. void 
  228. exposeCB(w,client_data,call_data)
  229. Widget w;
  230. caddr_t client_data;
  231. caddr_t call_data;
  232. {
  233.     GLXwinset(XtDisplay(w), XtWindow(w));
  234.     drawScene();
  235.  
  236. }
  237.  
  238. void
  239. initCB(w,client_data,call_data)
  240. Widget w;
  241. caddr_t client_data;
  242. caddr_t call_data;
  243. {
  244.     GLXwinset(XtDisplay(w), XtWindow(w));
  245.     zbuffer(TRUE);
  246.     setMatrix();
  247. }
  248.  
  249. void
  250. inputCB(w,client_data,glx_struct)
  251. Widget w;
  252. caddr_t client_data;
  253. GlxDrawCallbackStruct *glx_struct;
  254. {
  255.      switch(glx_struct->event->type) {
  256.      case ButtonPress :
  257.      break;
  258.      case ButtonRelease :
  259.          switch(glx_struct->event->xbutton.button){
  260.          case Button1 :
  261.          toSingleBuffer();
  262.          break;
  263.          case Button2 :
  264.          toDoubleBuffer();
  265.          break;
  266.          case Button3 :
  267.          loop();
  268.          break;
  269.          }
  270.      break;
  271.      }
  272. }
  273.  
  274.  
  275. void
  276. exitAC()
  277. {
  278.     gexit();
  279.     XtCloseDisplay(display);
  280.     exit(0);
  281. }
  282.  
  283.  
  284. void
  285. drawScene()
  286. {
  287.     static float sp1[] = {0.2,0.2,0.2,3.0};
  288.     static float sp2[] = {3.4,3.4,0.2,4.0};
  289.  
  290.     GLXwinset(XtDisplay(glw), XtWindow(glw));
  291.     RGBcolor(0,0,0);
  292.     clear();
  293.     zclear();
  294.  
  295.     pushmatrix();
  296.     lmbind(MATERIAL,1);
  297.     sphdraw(sp1);
  298.     rotate(ax,'x');
  299.     rotate(ay,'y');
  300.     rotate(az,'z');
  301.     lmbind(MATERIAL,2);
  302.     sphdraw(sp2);
  303.     popmatrix();
  304.  
  305.     if (doubleBuffer) swapbuffers();
  306. }
  307.  
  308. void
  309. toSingleBuffer()
  310. {
  311.     acnt = 0;
  312.     XtSetArg(args[acnt],XmNtitle, "SINGLE buffer");acnt++;
  313.     XtSetValues(toplevel,args,acnt);
  314.  
  315.     XtManageChild(frameSB);
  316.     XtUnmanageChild(frameDB);
  317.     glw = glwSB;
  318.     doubleBuffer = FALSE;
  319.     GLXwinset(XtDisplay(glw), XtWindow(glw));
  320.     setMatrix();
  321.     bindLight();
  322.     drawScene();
  323. }
  324.  
  325. void
  326. toDoubleBuffer()
  327. {
  328.     acnt = 0;
  329.     XtSetArg(args[acnt],XmNtitle, "DOUBLE buffer");acnt++;
  330.     XtSetValues(toplevel,args,acnt);
  331.  
  332.     XtManageChild(frameDB);
  333.     XtUnmanageChild(frameSB);
  334.     glw = glwDB;
  335.     doubleBuffer = TRUE;
  336.     GLXwinset(XtDisplay(glw), XtWindow(glw));
  337.     setMatrix();
  338.     bindLight();
  339.     drawScene();
  340.  
  341. }
  342.  
  343. void
  344. initialize()
  345. {
  346.     lmdef(DEFMATERIAL,1,11,material1);
  347.     lmdef(DEFMATERIAL,2,11,material2);
  348.     lmdef(DEFLIGHT,   1,14,white_light);
  349.     lmdef(DEFLMODEL,  1,7,infinite);
  350.  
  351.     lsetdepth(getgdesc(GD_ZMIN),getgdesc(GD_ZMAX));
  352.     shademodel(GOURAUD);
  353.     sphmode(SPH_PRIM,SPH_MESH);
  354.     sphmode(SPH_DEPTH,8);
  355.  
  356. }
  357.  
  358. void
  359. setMatrix()
  360. {
  361.     mmode(MPROJECTION);
  362.     ortho(-10.0,10.0,-10.0,10.0,-10.0,10.0);
  363.     mmode(MVIEWING);
  364.     loadmatrix(iden);
  365.     lookat(0.0,0.0,3.0,0.0,0.0,0.0,0);
  366. }
  367.  
  368. void
  369. bindLight()
  370. {
  371.     pushmatrix();
  372.     loadmatrix(iden);
  373.     lmbind(LIGHT0,1);
  374.     lmbind(LMODEL,1);
  375.     popmatrix();
  376. }
  377.  
  378. void
  379. loop()
  380. {
  381.     register int i;
  382.  
  383.     for (i=0;i<50;i++) {
  384.         ax += 50;
  385.         ay -= 25;
  386.         az += 50;
  387.         if (ax >= 3600)  ax = 0;
  388.         if (ay <= -3600) ay = 0;
  389.         if (az >= 3600)  az = 0;
  390.     drawScene();
  391.     }
  392. }
  393.